//
//  BDMapController.m
//  beifang
//
//  Created by Apple on 17/5/2.
//  Copyright © 2017年 郭凯. All rights reserved.
//

#import "BDMapController.h"

@interface BDMapController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>
@property(nonatomic,strong)BMKMapView *mapView;
@property (nonatomic,strong) BMKLocationService* locService;
@end

@implementation BDMapController
+(void)initialize{
    [super initialize];
    //个性化地图模板文件路径
    NSString* path = [[NSBundle mainBundle] pathForResource:@"custom_config_午夜蓝" ofType:@""];
    //设置个性化地图样式
    [BMKMapView customMapStyle:path];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-64)];
    self.mapView = mapView;
    [self.view addSubview:mapView];
    /*
        BMKMapTypeNone       = 0,               ///< 空白地图
        BMKMapTypeStandard   = 1,               ///< 标准地图
        BMKMapTypeSatellite  = 2,               ///< 卫星地图
     */
    _mapView.mapType = BMKMapTypeStandard;//设置地图为空白类型
    //打开实时路况图层
    [_mapView setTrafficEnabled:YES];
    //打开百度城市热力图图层（百度自有数据）
//    [_mapView setBaiduHeatMapEnabled:YES];
    
    [_mapView setLogoPosition:BMKLogoPositionLeftTop];//设置logo的位置
    
//    [_mapView setOverlookEnabled:YES];//开启仰角支持
    
    [_mapView setOverlooking:-45];//设置仰角度数
    
    [_mapView setZoomLevel:16];//设置地图显示级数
    
//    [_mapView setShowMapPoi:YES];//开启兴趣点
    _mapView.showsUserLocation = YES;//显示定位图层
    // 添加一个大头针
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor;
    coor.latitude = 39.915;
    coor.longitude = 116.404;
    annotation.coordinate = coor;
    annotation.title = @"这里是北京";
    annotation.subtitle = @"中华人民共和国天安门";
    [_mapView addAnnotation:annotation];
    
    
    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    _mapView.userTrackingMode =BMKUserTrackingModeFollow;
    //启动LocationService
    [_locService startUserLocationService];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    [BMKMapView enableCustomMapStyle:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
}

// Override
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        return newAnnotationView;
    }
    return nil;
}

//实现相关delegate 处理位置信息更新
//处理方向变更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    NSLog(@"heading is %@",userLocation.heading);
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
}
@end
